﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;
using AForge.Math.Geometry;



namespace Bildverarbeitung
{
    public class KuebrichAndiFilter : IFilter
    {
        Bitmap IFilter.GetImageWithDoor(Bitmap source)
        {
            HoughLineTransformation lineTransform = new HoughLineTransformation();
            // apply Hough line transofrm
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);

            UnmanagedImage tmpPicCol = UnmanagedImage.FromManagedImage(source);
            //UnmanagedImage tmpPicCol = UnmanagedImage.FromManagedImage((Bitmap)Bitmap.FromFile("serie (1).jpg"));
            UnmanagedImage tmpPicBW = filter.Apply(tmpPicCol);

            CannyEdgeDetector edge = new CannyEdgeDetector(20, 40);
            edge.ApplyInPlace(tmpPicBW);
            lineTransform.ProcessImage(tmpPicBW);
            Bitmap houghLineImage = lineTransform.ToBitmap();
            // get lines using relative intensity
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.6);

            Line[] linesL = new Line[lines.Length];

            for (int actLine = 0; actLine < lines.Length; actLine++)
            {
                // get line's radius and theta values
                int r = lines[actLine].Radius;
                double t = lines[actLine].Theta;

                // check if line is in lower part of the image
                if (r < 0)
                {
                    t += 180;
                    r = -r;
                }

                // convert degrees to radians
                t = (t / 180) * Math.PI;

                // get image centers (all coordinate are measured relative
                // to center)
                int w2 = tmpPicCol.Width / 2;
                int h2 = tmpPicCol.Height / 2;

                double x0 = 0, x1 = 0, y0 = 0, y1 = 0;

                if (lines[actLine].Theta != 0)
                {
                    // none-vertical line
                    x0 = -w2; // most left point
                    x1 = w2;  // most right point

                    // calculate corresponding y values
                    y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                    y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);
                }
                else
                {
                    // vertical line
                    x0 = lines[actLine].Radius;
                    x1 = lines[actLine].Radius;

                    y0 = h2;
                    y1 = -h2;
                }

                // draw line on the image
                Drawing.Line(tmpPicCol,
                    new IntPoint((int)x0 + w2, h2 - (int)y0),
                    new IntPoint((int)x1 + w2, h2 - (int)y1),
                    Color.Red);

                linesL[actLine] = Line.FromPoints(new IntPoint((int)x0 + w2, h2 - (int)y0), new IntPoint((int)x1 + w2, h2 - (int)y1));

            }
           
            foreach (Line line in linesL)
            {
                foreach (Line secondLine in linesL)
                {
                    if (!line.Equals(secondLine))
                    {
                        AForge.Point? p = line.GetIntersectionWith(secondLine);
                        if (p.HasValue)
                        {
                            AForge.Point ed = p.GetValueOrDefault();
                            Drawing.FillRectangle(tmpPicCol, new Rectangle((int)ed.X - 5, (int)ed.Y - 5, 10, 10), Color.Red);
                        }
                    }
                }
            }

            return tmpPicCol.ToManagedImage();
        }

        public String GetName()
        {
            return ("KuebrichAndi");
        }
    }
}
